package com.ca.ui.core.view.card;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ui.core.R;
import com.ca.ui.core.util.ImageUtils;

public class InfoCard extends ConstraintLayout {

    private String imageUrl;
    private String name;
    private String other;

    public ImageView imageView;
    public TextView nameView;
    public TextView otherView;

    public InfoCard(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public InfoCard(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public InfoCard(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.info_card_layout, this);
        imageView = view.findViewById(R.id.iv);
        nameView = view.findViewById(R.id.tv_name);
        otherView = view.findViewById(R.id.tv_other);

        if (attrs != null) {
            TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.InfoCard, 0, 0);
            imageUrl = array.getString(R.styleable.InfoCard_ic_imageUrl);
            name = array.getString(R.styleable.InfoCard_ic_name);
            other = array.getString(R.styleable.InfoCard_ic_other);

            array.recycle();
        }

        setOnClickListener(v -> {});
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl == null ? "" : imageUrl;
        ImageUtils.loadRoundImage(getContext(), this.imageUrl, imageView, 10);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? "" : name;
        this.nameView.setText(this.name);
    }

    public String getOther() {
        return other;
    }

    public void setOther(String other) {
        this.other = other == null ? "" : other;
        this.otherView.setText(this.other);
    }
}
